<?php

class Ebx_Helper_Calendar implements Ebx_Helper_Interface {

	protected $_options = array();

	protected $_time = null;
	protected $_locale = null;
	protected $_events = array();

	protected $_day = 0;
	protected $_month = 0;
	protected $_year = 0;
	protected $_weekDay = 0;

	protected $_curDay = 0;
	protected $_curMonth = 0;
	protected $_curYear = 0;
	protected $_curWeekDay = 0;

	function __construct($options = array(), $time = null) {
		$this->setTime($time);
		$this->_init();
		
		$this->setOptions($options);
	}

	function setOptions($options) {
		foreach ($options as $key => $value) {
			switch ($key) {
				case 'year':
					$this->setYear($value);
					unset($options[$key]);
				break;

				case 'month':
					$this->setMonth($value);
					unset($options[$key]);
				break;

				case 'day':
					$this->setDay($value);
					unset($options[$key]);
				break;

				case 'time':
					$this->setTime($value);
					unset($options[$key]);
				break;

				case 'events':
					$this->setEvents($value);
					unset($options[$key]);
				break;
			}
		}
		$this->_options = (array)$options;
	}

	function getOptions() {
		return $this->_options;
	}

	function setTime($time) {
		if ($time !== null) {
			$this->_time = (int)$time;
		}
	}

	function getTime() {
		return $this->_time;
	}

	function setYear($year) {
		$this->_curYear = (int)$year;
	}

	function getYear() {
		return $this->_curYear > 0 ? $this->_curYear : $this->_year;
	}

	function setMonth($month) {
		$month = (int)$month;
		if ($month < 1 || $month > 12) {
			$month = $this->_month;
		}
		$this->_curMonth = $month;
	}

	function getMonth() {
		return $this->_curMonth > 0 ? $this->_curMonth : $this->_month;
	}

	function setDay($day) {
		$this->_curDay = (int)$day;
	}

	function getDay() {
		return $this->_curDay > 0 ? $this->_curDay : $this->_day;
	}

	function getDate($day = null, $month = null, $year = null) {
		$day = $day !== null ? (int)$day : $this->getDay();
		$month = $month !== null ? (int)$month : $this->getMonth();
		$year = $year !== null ? (int)$year : $this->getYear();
		return mktime(0,0,0, $month, $day, $year);
	}

	function getWeekDay() {
		return date('N', $this->getDate());
	}

	function getMonthName($type = 'full', $month = null) {
		if ($this->_locale !== null) {
			$month = $month ? (int)$month : $this->getMonth();
			return $this->_locale['monthNames'][$type][$month];
		} else {
			return $this->getMonth();
		}
	}

	function getDayName($type = 'full', $day = null) {
		if ($this->_locale !== null) {
			$day = $day ? (int)$day : $this->getWeekDay();
			return $this->_locale['dayNames'][$type][$day];
		} else {
			return $this->getWeekDay();
		}		
	}

	function getMonths($type = 'full') {
		return $this->_locale['monthNames'][$type];
	}

	function getDays($type = 'full') {
		return $this->_locale['dayNames'][$type];
	}

	function setEvents($events = null) {
		if (is_array($events)) {
			$this->_events = $events;
		}
	}

	function getDayEvents($day, $month = null, $year = null) {
		if ($month === null) {
			$month = $this->_month;
		}

		if ($year === null) {
			$year = $this->_year;
		}

		if (isset($this->_events[$year][$month][$day])) {
			return $this->_events[$year][$month][$day];
		}
	}

	function getDayEvent($event, $day, $month = null, $year = null) {
		$events = $this->getDayEvents($day, $month, $year);
		return isset($events[$event]) ? $events[$event] : null;
	}

	function direct($events, $options = null) {

		$options = $this->getOptions();

		$curDay = $this->getDay();
		$curMonth = $this->getMonth();
		$curYear = $this->getYear();
		$monthSinceTime = mktime(0,0,0,$curMonth,1,$curYear);
		$numDays = date('t', $monthSinceTime);

		//fix day
		if ($curDay < 1 || $curDay > $numDays) {
			$curDay = $this->_day;
			$this->setDay($curDay);
		}

		$curWeekDay = $this->getWeekDay();
		$firstDay = date('N', $monthSinceTime);

		$monthFormat = isset($options['MonthFormat']) ? $options['MonthFormat'] : 'full';
		$weekDaysFormat = isset($options['DaysFormat']) ? $options['DaysFormat'] : 'full';

		$months = $this->getMonths($monthFormat);
		$weekDays = $this->getDays($weekDaysFormat);

		$result = array();

		$rows = ceil($numDays / 7);
		$cols = 7;
		$class = isset($options['colClass']) ? $options['colClass'] : '';
		$dateFormat = isset($options['dateFormat']) ? $options['dateFormat'] : false;
		$dayUrl = isset($options['dayUrl']) ? $options['dayUrl'] : false;

		for ($row = 1, $day = 1; $row <= $rows; $row++) {
			for ($col = 1; $col <= $cols; $col++, $day++) {
				if ($firstDay > $col) {
					$result['days'][$row][$col] = array(
						'empty' => true
					);
					continue;
				}

				$result['days'][$row][$col] = array(
					'empty' 	=> false,
					'num' 		=> $day,
					'current' 	=> $day == $this->_day && $curMonth == $this->_month && $curYear == $this->_year,
					'selected' 	=> $day == $curDay,
					'class' 	=> $class,
					'day'		=>  $this->getDayName($weekDaysFormat, $col),
					'format' 	=> ($dateFormat !== false
									? Ebx_HelperBroker::helper('PrettyDateFormat', $this->getDate($day,$curMonth,$curYear), array('format' => $dateFormat))
									: false),

					'url'		=> ($dayUrl !== false
									? Ebx_Project_Tools::getUrl(array(
										'YEAR' => $curYear,
										'MONTH' => $curMonth,
										'DAY' => $day
								  	), $dayUrl)
				                	: false),

					'events'	=> ($events ? $this->getDayEvents($day) : false)
				);

			}
		}

		foreach ($months as $key => $name) {
			$result['months'][$key] = array(
				'month' 	=> $name,
				'current' 	=> $key == $this->_month,
				'selected' 	=> $key == $curMonth,
			);
		}

		return $result;

	}

	protected function _init() {
		//time option need for set current region time
		$time = $this->getTime();
		if ($time === null) {
			$time = time();
		}

		$this->_day = date('j', $time);
		$this->_month = date('n', $time);
		$this->_year = date('Y', $time);
		$this->_weekDay = date('N', $time);
		
		$this->_locale = Ebx_Project_Tools::getLangOptions(__CLASS__);

	}

}

?>